package com.alameda.service.manage;

import com.alameda.api.manage.BusinessIndicatorService;
import com.alameda.api.statement.SystemRecordService;
import com.alameda.manage.bo.BusinessIndicatorBO;
import com.alameda.manage.dto.BusinessIndicatorDTO;
import com.alameda.manage.enums.StateEnum;
import com.alameda.manage.pojo.BusinessIndicatorDO;
import com.alameda.manage.vo.BusinessIndicatorVO;
import com.alameda.mapper.manage.BusinessIndicatorMapper;
import com.alameda.statement.enums.SystemRecordEnum;
import com.alameda.utils.ExcelUtils;
import com.alameda.utils.MapUtils;
import com.alameda.utils.ObjectUtils;
import com.alameda.utils.PubUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 技术指标 业务逻辑层 具体实现
 *
 * @author Lv
 * @date 2024/08/04
 */
@Slf4j
@Service
public class BusinessIndicatorServiceImpl extends ServiceImpl<BusinessIndicatorMapper, BusinessIndicatorDO> implements BusinessIndicatorService {

    @Autowired
    private BusinessIndicatorMapper businessIndicatorMapper;

    /**
     * 查询业务指标列表
     *
     * @param businessIndicatorDTO 技术指标数据传递对象
     * @return
     */
    @Override
    public IPage<BusinessIndicatorVO> listBusinessIndicator(BusinessIndicatorDTO businessIndicatorDTO) {
        IPage<BusinessIndicatorDO> iPage = new Page<>(businessIndicatorDTO.getPageNum(), businessIndicatorDTO.getPageSize());

        IPage<BusinessIndicatorVO> pageData = businessIndicatorMapper.findBusinessIndicatorInfo(iPage,
                businessIndicatorDTO.getProjectId(),
                businessIndicatorDTO.getBusinessIndicatorRemark(),
                businessIndicatorDTO.getState(),
                businessIndicatorDTO.getIndexNo(),
                businessIndicatorDTO.getPriority());

        pageData.getRecords().forEach(item -> item.setStateRemark(StateEnum.getValueByKey(item.getState())));
        return pageData;
    }

    @Autowired
    private SystemRecordService systemRecordService;

    /**
     * 添加或修改业务指标
     *
     * @param businessIndicatorDTO 技术指标数据传递对象
     * @return
     */
    @Override
    public Boolean saveOrUpdateBusinessIndicator(BusinessIndicatorDTO businessIndicatorDTO) {
        BusinessIndicatorDO businessIndicatorDO = ObjectUtils.convert(businessIndicatorDTO, BusinessIndicatorDO.class);

        Map<String, String> extData = Optional.ofNullable(businessIndicatorDTO.getExtData()).orElse(MapUtils.newHashMap());
        if (PubUtils.isNull(businessIndicatorDTO.getId())) {
            // 新增场景
            extData.put("indexNo", getIndexNo());
            extData.put("top", businessIndicatorDTO.getPriority());
        } else {
            // 修改场景
            extData.put("top", businessIndicatorDTO.getPriority());
        }
        businessIndicatorDO.setExtData(extData);

        systemRecordService.saveRecord(SystemRecordEnum.BUSINESS_COUNT);
        return this.saveOrUpdate(businessIndicatorDO);
    }

    /**
     * 获取业务编号
     *
     * @return
     */
    private String getIndexNo() {
        Date date = new Date();
        String format = "yyyyMMddHHmmss";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        String formatDate = simpleDateFormat.format(date);
        return "Bu-" + formatDate;
    }

    /**
     * 导出业务指标数据
     *
     * @param businessIndicatorDTO 技术指标数据传递对象
     */
    @Override
    public void export(BusinessIndicatorDTO businessIndicatorDTO) {
        List<BusinessIndicatorVO> businessIndicatorVOList = getExportData(businessIndicatorDTO);

        List<BusinessIndicatorBO> businessIndicatorBOList = businessIndicatorVOList.stream().map(item -> {
            BusinessIndicatorBO businessIndicatorBO = ObjectUtils.convert(item, BusinessIndicatorBO.class);
            businessIndicatorBO.setProjectName(item.getProjectName());
            businessIndicatorBO.setState(StateEnum.getValueByKey(item.getState()));
            businessIndicatorBO.setPriority(Optional.ofNullable(item.getExtData()).orElse(MapUtils.newHashMap()).get("top"));
            businessIndicatorBO.setIndexNo(Optional.ofNullable(item.getExtData()).orElse(MapUtils.newHashMap()).get("indexNo"));
            return businessIndicatorBO;
        }).collect(Collectors.toList());

        ExcelUtils.export(businessIndicatorBOList, BusinessIndicatorBO.class, "业务指标信息");
    }

    /**
     * 通过项目id集合删除
     *
     * @param projectIdList id集合
     * @return
     */
    @Override
    public Boolean removeByProjectIdList(List<Long> projectIdList) {
        LambdaUpdateWrapper<BusinessIndicatorDO> wrapper = Wrappers.lambdaUpdate(BusinessIndicatorDO.class)
                .in(BusinessIndicatorDO::getProjectId, projectIdList);
        return this.remove(wrapper);
    }

    /**
     * 获取导出数据
     *
     * @param businessIndicatorDTO 技术指标数据传递对象
     * @return
     */
    private List<BusinessIndicatorVO> getExportData(BusinessIndicatorDTO businessIndicatorDTO) {
        if (PubUtils.isNotEmpty(businessIndicatorDTO.getIdList())) {
            return businessIndicatorMapper.findByIdIn(businessIndicatorDTO.getIdList());
        } else {
            IPage<BusinessIndicatorVO> pageData = listBusinessIndicator(businessIndicatorDTO);
            List<Long> idList = pageData.getRecords().stream().map(BusinessIndicatorVO::getId).collect(Collectors.toList());
            return businessIndicatorMapper.findByIdIn(idList);
        }
    }
}
